//存放交互逻辑
// js模块化
var seckill = {
    URL: {
        now: function () {
            return '/seckill/time/now';
        },
        exposer:function (seckillId) {
          return '/seckill/'+seckillId+'/exposer';
        },
        execution:function (seckillId,md5) {
            return '/seckill/'+seckillId+'/'+md5+'/'+'execution';
        }
    },
    handleSeckill: function (seckillId,node) {
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');

        $.post(seckill.URL.ecposer(seckillId),{},function (res) {

            if (res && res['success']) {
                var exposer = res['data'];

                if (exposer['exposed']) {
                    var md5 = res['md5'];
                    var killUrl = seckill.URL.execution(seckillId.md5);
                    //绑定一次点击事件
                    $('#killBtn').one('click',function () {
                       // 执行秒杀请求
                        // 1 禁用按钮
                       $(this).addClass('disabled');
                       //2 发送秒杀请求
                        $.post(killUrl,{},function (res) {
                            if (res && res['success']) {
                             var killRes = res['data'];
                             var state = killRes.state;
                             var stateInfo = killRes.stateInfo;
                             //3 显示秒杀结果
                                node.html('<span class="label label-success">'+stateInfo+'</span>')
                            }
                        });
                    });

                } else {
                    var now = res['now'];
                    var start = res['start'];
                    var end = res['end'];
                    seckill.countdown(seckillId,now,start,end);
                }
            } else {

            }
        });
    },
    validatePhone: function (phone) {
        if (phone && phone.length === 11 && !isNaN(phone)) {
            return true;
        }
        return false;
    },
    countdown: function (seckillId, nowTime, startTime, endTime) {
        var seckillBox = $('#seckill-box');
        if (nowTime > endTime) {
            //秒杀结束
            seckillBox.html('秒杀结束了');
        } else if (nowTime < startTime) {
            //未开始
            var killTime = new Date(startTime + 1000);
            seckillBox.countdown(killTime, function (event) {

                var format = event.strftime('秒杀倒计时： %D天 %H时 %M分 %S秒');
                seckillBox.html(format);
            }).on('finish.countdown', function () {
                // 获取秒杀地址，控制显示逻辑，执行秒杀
                seckill.handleSeckill(seckillId,seckillBox);
            });
        } else {
            //开始
            seckill.handleSeckill(seckillId,seckillBox);
        }
    },
    detail: {
        init: function (params) {
            // 用户手机验证，及时交互
            //在cookie中查找手机号
            var killPhone = $.cookie('killPhone');
            var startTime = params('startTime');
            var endTime = params('endTime');
            var seckillId = params('seckillId');
            if (seckill.validatePhone(killPhone)) {
                var killPhoneModal = $('#killPhoneModal');
                killPhoneModal.modal({
                    show: true,
                    backdrop: 'static',
                    keyboard: false
                });
                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killPhoneKey').val();
                    if (seckill.validatePhone(inputPhone)) {
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'});
                        window.location.reload();
                    } else {
                        $('#killPhoneMsg').hide().html('<label class="label lable-danger">手机号错误</label>').show(300);
                    }
                });
            }
            // 已经登录
            // 计时交互
            $.get(seckill.URL.now(), {}, function (res) {
                if (res && res['success']) {
                    var nowTime = res['data'];
                    seckill.countdown(seckillId, nowTime, startTime, endTime);
                }
            })

        }
    }
}